package com.best.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.best.mapper.AppUserMapper;
import com.best.mapper.FoodMapper;
import com.best.mapper.LoveFoodMapper;
import com.best.pojo.AppUser;
import com.best.pojo.Food;
import com.best.pojo.LoveFood;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangruyi
 * @date 2021/3/10
 */
@Service
public class UserService {
    @Autowired
    private AppUserMapper userMapper;

    @Autowired
    private LoveFoodMapper loveFoodMapper;
    @Autowired
    private FoodMapper foodMapper;
    public boolean addUser(AppUser user) {
        return userMapper.insert(user) >= 1;
    }
    public boolean isRepeated(String username){
        QueryWrapper<AppUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AppUser::getUsername,username);
        return userMapper.selectCount(wrapper) >= 1;
    }
    public boolean updateUser(AppUser user){
        return userMapper.updateById(user)>=1;
    }
    public AppUser detail(String id){
        return userMapper.selectById(id);
    }
    public List<Food> lovesOfUser(String userId){
        QueryWrapper<LoveFood> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(LoveFood::getUserId,userId);
        List<LoveFood> loveFoods = loveFoodMapper.selectList(wrapper);
        if(loveFoods == null||loveFoods.size() == 0){
            return null;
        }
        List<Food> list = new ArrayList<>();
        loveFoods.forEach(love ->{
            Food food = foodMapper.selectById(love.getFoodId());
            list.add(food);
        });
        if(list.size() == 0){
            return null;
        }
        return list;
    }
    public AppUser authentication(String username){
        QueryWrapper<AppUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AppUser::getUsername,username);
        return userMapper.selectOne(wrapper);
    }
}
